package com.core.life.service.impl;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.common.dao.Pager;
import com.core.common.pageModel.Combo;
import com.core.life.dao.IConsumeItemDao;
import com.core.life.dao.IConsumeRecordDao;
import com.core.life.entity.ConsumeItemEntity;
import com.core.life.service.IConsumeItemService;
import com.core.life.vo.ConsumeItemVo;
import com.utils.date.DateUtil;

@Service
public class ConsumeItemServiceImpl implements IConsumeItemService{

	@Resource
	private IConsumeItemDao consumeItemDao;
	@Resource
	private IConsumeRecordDao consumeRecordDao;
	
	@Transactional
	@Override
	public void save(ConsumeItemVo item) {
		ConsumeItemEntity entity = new ConsumeItemEntity();
		entity.setUpdateDate(entity.getCreateDate());
		entity.setName(item.getName());
		consumeItemDao.save(entity);
	}

	@Transactional
	@Override
	public void delete(String strIds) {
		if(StringUtils.isNotBlank(strIds)){
			String[] idArray = strIds.split(",");
			List<Long> itemIds = new ArrayList<Long>();
			for (String id : idArray) {
				itemIds.add(Long.parseLong(id));
			}
			//consumeRecordDao.deleteByConsumeItemIds(itemIds);
			consumeItemDao.deleteByIds(itemIds);			
		}
	}

	@Transactional
	@Override
	public void update(ConsumeItemVo item) {
		ConsumeItemEntity entity = consumeItemDao.get(item.getId());
		if(null != entity){
			entity.setUpdateDate(new Date());
			entity.setName(item.getName());
			consumeItemDao.update(entity);			
		}
	}
	
	@Override
	public boolean existTypeName(ConsumeItemVo item) {
		List<ConsumeItemEntity> entityList = consumeItemDao.findByParam("name", item.getName());
		if(CollectionUtils.isNotEmpty(entityList)){
			if(item.getId() == 0 || item.getId() != entityList.get(0).getId())
				return true;
		}
		return false;
	}

	@Override
	public Pager<ConsumeItemVo> getList(int pageIndex, int pageSize, String sort, String order, Map<String,Object> optionMap) {
		Pager<ConsumeItemEntity> pager = consumeItemDao.selectByOption(pageIndex, pageSize, sort, order);
		List<ConsumeItemEntity> entityList = pager.getDatas();
		if(CollectionUtils.isNotEmpty(entityList)){
			List<ConsumeItemVo> voList = new ArrayList<ConsumeItemVo>(entityList.size());
			ConsumeItemVo vo = null;
			for (ConsumeItemEntity entity : entityList) {
				vo = new ConsumeItemVo();
				vo.setId(entity.getId());
				vo.setName(entity.getName());
				vo.setCreateDate(DateUtil.format(entity.getCreateDate()));
				voList.add(vo);
			}
			return new Pager<ConsumeItemVo>(pageIndex, pageSize, pager.getTotalCount(), voList);
		}
		return null;
	}

    @Override
    public Pager<ConsumeItemVo> getList(Map<String, Object> optionMap) {
        Pager<ConsumeItemEntity> pager = consumeItemDao.find(optionMap);
        if(null != pager){
            List<ConsumeItemEntity> entityList = pager.getDatas();
            if(CollectionUtils.isNotEmpty(entityList)){
                List<ConsumeItemVo> voList = entityList
                        .stream()
                        .map(entity -> new ConsumeItemVo(entity.getId(),entity.getName(),DateUtil.format(entity.getCreateDate())))
                        .collect(Collectors.toList());
                return new Pager<ConsumeItemVo>(pager.getPageIndex(), pager.getPageSize(), pager.getTotalCount(), voList);
            }
        }
        return null;
    }
    
	@Override
	public List<Combo> getConsumeItemComboList(Long id) {
		List<ConsumeItemEntity> list = consumeItemDao.findAll();
		if(CollectionUtils.isNotEmpty(list)){
			List<Combo> comboList = new ArrayList<Combo>(list.size());
			Combo combo = null;
			for (ConsumeItemEntity entity : list) {
				combo = new Combo();
				combo.setValue(String.valueOf(entity.getId()));
				combo.setText(entity.getName());
				if(null != id && id == entity.getId())
					combo.setSelected(true);
				comboList.add(combo);
			}
			return comboList;
			
		}
		return null;
	}

}
